var cur_competitions_page = 0;
var cur_competitions_sort_key = 'time';
var cur_competitions_sort = 'DESC';

var cur_question_page = 0;
var cur_question_sort_type = 'hot';
var bp_question_more_inner_o = '';

var cur_log_page = 0;

function reloadQuestionSortList(el, sort_type)
{
	cur_question_page = 0;
	cur_question_sort_type = sort_type;
	
	$('#sort_question_control a').removeClass();
	
	$(el).addClass('cur');
	
	$('#c_question_list').html('<p style="padding: 15px 0" align="center"><img src="' + G_STATIC_URL + '/common/loading_b.gif" alt="" /></p>');
	
	$('#bp_question_more').html(bp_question_more_inner_o).click();
}

function reloadCompetitionsSortList(el, sort_key)
{
	cur_competitions_page = 0;
	cur_competitions_sort_key = sort_key;
	
	switch (el.className)
	{
		default:
		case 'on':
			cur_competitions_sort = 'DESC';
		break;
		
		case 'cur':
			cur_competitions_sort = 'ASC';
		break;
	}
	
	$('#sort_competitions_control a').removeClass();
	
	switch (cur_competitions_sort)
	{
		case 'ASC':
			$(el).addClass('on');
		break;
		
		case 'DESC':
			$(el).addClass('cur');
		break;
	}
	
	$('#c_competitions_list').html('<p style="padding: 15px 0" align="center"><img src="' + G_STATIC_URL + '/common/loading_b.gif" alt="" /></p>');
	
	$('#bp_competitions_more').html(bp_competitions_more_inner_o).click();
}

$(document).ready(function(){
	if (document.getElementById('c_all_list'))
	{
		bp_more_load(G_BASE_URL + '/topic/ajax/get_all/topic_id-' + TOPIC_ID, $('#bp_all_more'), $('#c_all_list'));
		
		bp_more_load(G_BASE_URL + '/topic/ajax/get_all_best_question/topic_id-' + TOPIC_ID, $('#bp_best_question_more'), $('#c_best_question_list'));
		
		bp_more_load(G_BASE_URL + '/question/ajax/discuss/template-__answer_count-0__page-0__category-__version-2__topic_id-' + TOPIC_ID, $('#bp_noanswer_more'), $('#c_noanswer_list'));
	}
	
	if (document.getElementById('focus_users'))
	{
		$.get(G_BASE_URL + '/topic/ajax/get_focus_users/topic_id-' + TOPIC_ID, function (data) {
			$.each(data, function (i, d) {		
				$('#focus_users').append('<a href="'+G_BASE_URL + '/people/' + d['user_name'] + '" ><img src="' + d['avatar_file'] + '" onMouseOver="eventsMouseM(this);" rel="'+d['uid']+'" /></a>&nbsp;');
			});
		}, 'json');
	}

	
	if (document.getElementById('topic_pic_uploader'))
	{		
		init_img_uploader(G_BASE_URL + '/topic/ajax/upload_topic_pic/topic_id-' + TOPIC_ID, 'topic_pic', $('#topic_pic_uploader'), false, $('#topic_pic'));
	}
});

function _topic_experience_editor__processer(result)
{
	if (result.errno != 1)
	{
		qAlert(result.err);
	}
	else
	{
		$('#topic_experience span').html(result.err);
		$('#topic_experience_editor textarea').val(result.err);
		$('#topic_experience_editor form p.default_save a').first().click();
	}
}

(function(){

var doc = document,f = function(k,c,g,e,l,ck,cn){
	
	var o = k.getElementsByTagName(l);
	
		for(var i=-1,len= o.length; ++i < len ;){
			
			(function(i){
				var el = o[i];
				
				el[ck] = function(){
					
					for(var n=-1,len=o.length; ++n < len ;){
					
						var m = doc.getElementById(e+n);
						
						m[cn]= g;
						o[n][cn]='';
					}
					var m = doc.getElementById(e+i);
					
					m[cn]= c;
					el[cn]='current';
				}
				
			})(i)
		}
	},
	
	uLister = doc.getElementById("uLister"),
	uclassCur = 'detaListerDiv show', uclassHide = 'detaListerDiv hide';
	
	
	//f(uLister,uclassCur,uclassHide,"layer","a",'onclick','className');

})();



function hide(o,j,c,p,x){
	
	var addtxt = d.getElementById("addtxt");
	var x = d.getElementById(o);
	var xjx = d.getElementById(j);
		x.className = c;
		xjx.className = p;
	var em = addtxt.getElementsByTagName("em");	
	for(var i =-1, len = em.length; ++i<len; ){
		if (j == 'editorTxt'){
			em[i].className = '';
			em[i].parentNode.style.paddingRight='15px';
		}else{
			em[i].parentNode.style.paddingRight='6px';
			em[i].className = 'hide';
		}
	}
	
	
}

function addElementVALUE(){

		var inps = d.getElementById('editorInput').getElementsByTagName("input")[0],classname=null;
		var addtxt = d.getElementById('addtxt');
		var s_span = addtxt.getElementsByTagName("span");
		
		if (inps.className =='i_add'){
			
			classname = inps;
		}
		
		var inpsValue = classname.value;//.replace(/\s/g,'');
		
		if (inpsValue==''){return false;}
		
		for(var xi = 0,len = s_span.length; xi <len ; xi++){
			
			var s = s_span[xi];
			if (s.getAttribute("val") == inpsValue){
				
				return false;
			}
		}
		
		var xmlHttp,xmlValue;		
				xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("microsoft.XMLHTTP");
				psotUrl = G_BASE_URL + '/topic/ajax/save_related_topic/topic_id-' + TOPIC_ID;	　
				xmlHttp.open("post",psotUrl,true);　　　　
				xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");　　　　
				xmlHttp.onreadystatechange=function(){			
					 if ( xmlHttp.readyState == 4 && xmlHttp.status == 200 ){		 
						var x = new Function('return'+xmlHttp.responseText)();
						if (x.errno ==1){
								d.getElementById('addtxt').appendChild(m.mx().add(inpsValue,x.rsm.related_id));
							
						}else if (x.errno ==-1){
							d.getElementById('editorInput').className="editorInput";
							d.getElementById('editor').className="editor hide";
							doc.getElementById("err").innerHTML = x.err;
							doc.getElementById("err").className = 'cr';
							var em = addtxt.getElementsByTagName("em");
		
							for(var i =-1, len = em.length; ++i<len; ){
								
								em[i].className = '';
							}
						}		
				}
							
			};　　
			
			xmlHttp.send('topic_title='+inpsValue);
}



var removeChilds = function(x){
	var el = x;
	var delectURL = G_BASE_URL + '/topic/ajax/remove_related_topic/related_id-'+x.parentNode.getAttribute("rel")+'__topic_id-'+TOPIC_ID;
	var xmlHttp;			
	xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("microsoft.XMLHTTP");
	xmlHttp.open("post",delectURL,true);　　　　
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");　　　　
	xmlHttp.onreadystatechange=function(){
		 if ( xmlHttp.readyState == 4 && xmlHttp.status == 200 ){
			var t = xmlHttp.responseText;
			var x = new Function('return'+t)();
			el.parentNode.parentNode.removeChild(el.parentNode);
		}
		
	};　
	xmlHttp.send(null);
}



var m = {
	
		mx : function (o){
			try{
			var doc = document, 
				mxId = doc.getElementById("Manytopics"),
				ajaxDate = doc.getElementById("ajaxDate"),
				take = doc.getElementById("take"),
				txt = doc.getElementById("txt"),
				addtxt = doc.getElementById("addtxt");
			
			var mxinput = mxId.getElementsByTagName("input")[0];
			var mxDiv = mxId.getElementsByTagName("div")[0];
			var mxSpan = mxDiv.getElementsByTagName("span");
			var eventLister = 'onkeydown' && 'onkeyup';
			
			}catch(e){return;}
			var el = this;
			
			return {	
				add : function (val,id){
					var s = '<a val="'+val+'" style="padding-right:15px;" href="'+G_BASE_URL + '/topic/'+id+'" rel="'+id+'">'+val+'<em onclick="removeChilds(this); return false;">x</em></a>';
						return s ;
				},
				
				app : function (x){
						var objValue = x.getAttribute("date-list");
						var el = this;
						var xmlHttp,xmlValue;
						xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("microsoft.XMLHTTP");
						psotUrl = G_BASE_URL + '/topic/ajax/save_related_topic/topic_id-'+TOPIC_ID;
						　
						xmlHttp.open("post",psotUrl,true);　　　　
						xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");　　　　
						xmlHttp.onreadystatechange=function(){
							
							 if ( xmlHttp.readyState == 4 && xmlHttp.status == 200 ){
								var t = xmlHttp.responseText;
								var x = new Function('return'+t)();
								if (x.errno ==1){
									var s = el.add(objValue,x.rsm.related_id);
									addtxt.appendChild(s);
								}else if (x.errno ==-1){
									
									d.getElementById('editorInput').className="editorInput";
									d.getElementById('editorTxt').className="editorTxt hide";
									doc.getElementById("err").innerHTML = x.err;
									doc.getElementById("err").className = 'cr';
									var em = addtxt.getElementsByTagName("em");
				
									for(var i =-1, len = em.length; ++i<len; ){
										
										em[i].className = '';
									}
								}
								
							}
							
						};　　
						xmlHttp.send('topic_title='+objValue);
						
						ajaxDate.className = 'hide';
						mxinput.value ='';
						mxinput.focus();
				},
				
				ev : function( o ){
					var elx = this;
					
					o[eventLister] = function(){
						
						mxinput.classNam ='i_add';
						doc.getElementById("err").className = 'cr hide';
						if (this.value.replace(/\s/g,'') =='') {ajaxDate.className = 'hide';return false;}
						var xmlvalue,XHR;
						var objValue = this.value ;
						var strValue = objValue.toString();
						var stv = strValue.replace(/\s/g,'');
						ajaxDate.className ='';
						take.setAttribute("date-list",stv);
						take.innerHTML = stv;
						var url = G_BASE_URL + '/search/ajax/search/type-topic_add__q-'+stv+'__limit-100';

						
							XHR = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("microsoft.XMLHTTP");
							XHR.open("GET",url,true); 
							XHR.onreadystatechange = function(){
							
							 if ( XHR.readyState == 4 && XHR.status == 200 ){
									
									xmlvalue = XHR.responseText;
									var x = new Function('return'+xmlvalue)();
									if (x.length >0){
										for(var i=-1,len= x.length; ++i<len;){
											
											//判断是否相同的话题，否则出现添加话题
											if (i!==0){
												
													txt.getElementsByTagName("span")[0].className =' hide';
												var elementsAjax = doc.createElement("div");
													elementsAjax.onclick = function(){ //ie7模式
														add(this.id);
													};
													elementsAjax.id =  x[i].sno;
													elementsAjax.setAttribute("date-list",x[i].name);
													elementsAjax.innerHTML = x[i].name+'<p>关注：<span>'+x[i].detail.focus_count+'</span> 人</p>';
													txt.appendChild(elementsAjax);	
												
											}else if (i==0){
												
												if (x[i].exist == 0){
													take.parentNode.className = 'add';
													txt.getElementsByTagName("span")[0].className =' hide';
												}else if (x[i].exist == 1){
													take.parentNode.className = 'add hide';
												}
											}
										
										}
									}else{
										take.parentNode.className = 'add';
										txt.getElementsByTagName("span")[0].className =' hide';
									}

							   }else{
								
									txt.innerHTML = "<span style='color:#999;'><img src='"+G_STATIC_URL+"/common/load.gif'/> 正在加载..</span>";
								
							   }

							};
							XHR.send(null);	
					}
					
					doc.onclick = function(){
						ajaxDate.className = 'hide';
					}
					
				},
				
				fc : function(e){
				
					e.ev(mxinput);
				},
				info : function(){
				
					if (mxId.parentNode.nodeName === "LI"){
						mxId.parentNode.appendChild(d);
					}
					var el = this;
			        try{
						mxId['onclick'] = function(){ mxinput.focus();}
						mxinput['onfocus'] = function(){el.fc(el)};
						
					}catch(w){}
					
				}
				
			
			}
			
			
		},
		
		remove : function(){
		
			var o = this;
			o.parentNode.parentNode.removeChild(o.parentNode);
			
		}
	}


var t = new m.mx();t.info();


function add(x){
	var o = 'string' == typeof x ? document.getElementById(x) : x;
	t.app(o);
}



//话题详情页面，话题修改记录。

function amendantRecord(message)
	{
	var title= '<a href="javascript:;" onclick="hidePupBox(\'w_tagPupD\',\'w_mask\');" class="close_ti" title="点击关闭对话框"><em>close</em></a>话题修改记录';
	var html = '<div class="amendantRecord">' + message + '</div>';
	
	var logbox = new DialogBox_show(500, html, title,'',function(){
		var w_tagPupD = AW.getId('w_tagPupD');
		w_tagPupD.style.marginTop = '-130px';
		w_tagPupD.style.position = 'absolute';
	});
}


//话题标题修改
function Headliner(n,txtId,valId){
	var topic_txtid = document.getElementById(txtId);
	var topic_valId = document.getElementById(valId);
	var topic_inputtxt = document.getElementById('topic_txt');
	var topic_Titles = topic_valId.getElementsByTagName('em')[0];
	//topic_inputtxt.value=topic_Titles.getAttribute('rel');
	topic_inputtxt.setAttribute('value',topic_Titles.getAttribute('rel'));
	
	if(n){
		topic_txtid.className='';
		topic_valId.className='hide';
	}else{
		topic_txtid.className='hide';
		topic_valId.className='';
		var x = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("microsoft.XMLHTTP");
		psotUrl = G_BASE_URL + '/topic/ajax/modfiy_topic_title/topic_id-'+TOPIC_ID;
		　
		x.open("post",psotUrl,true);　　　　
		x.setRequestHeader("Content-Type","application/x-www-form-urlencoded");　　　　
		x.onreadystatechange=function(){		
			 if ( x.readyState == 4 && x.status == 200 ){
				var _v2 = new Function('return'+x.responseText)();
				if(_v2.errno==1){
					//alert(document.getElementById('topic_txt').value)
					topic_Titles.rel=document.getElementById('topic_txt').value;
					topic_Titles.innerHTML=document.getElementById('topic_txt').value;
				}else if(_v2.errno==-1){
					alert(_v2.err);
				}
			}
			
		};　　
		x.send('topic_title='+document.getElementById('topic_txt').value);
	}
}
